package action.grade;


import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

import service.queryGradeService;
import service.queryStudentService;
import service.queryWorkService;
import service.submitWorkService;

import com.opensymphony.xwork2.ActionSupport;

import model.Student;
import model.Work;
import model.submitRecord;

@Namespace("")
@Action(
	value="grade",
	results={
			@Result(name="success",location="/WEB-INF/grade.jsp")
	}		
)
public class Grade extends ActionSupport{
	public String execute() throws Exception{ 
	    ArrayList<submitRecord> submitlist=new ArrayList<submitRecord>();
	    ArrayList<Work> worklist=new ArrayList<Work>();
	    ArrayList<Student> studentlist=new ArrayList<Student>();
	    queryWorkService qws=new queryWorkService();
		submitWorkService sws=new submitWorkService();
		queryStudentService qss=new queryStudentService();
		queryGradeService qgs=new queryGradeService();
	    worklist=qws.queryAllWork();
	    HttpServletRequest request=ServletActionContext.getRequest();
		ServletContext application=request.getServletContext();
		String schoolyear=(String) application.getAttribute("schoolyear");
        studentlist=qss.queryAllStudent(schoolyear);
	    for(Work work:worklist){
	    	int work_id=work.getWork_id();
	    	for(Student student:studentlist){
	    		String stu_id=student.getStu_id();
	    		submitRecord record=new submitRecord();
	    		record.setWork_id(work_id);
	    		record.setStu_id(stu_id);
	    		record.setName(student.getName());
	    		int grade=qgs.queryGrade(work_id,stu_id,schoolyear);
	    		record.setGrade(grade);
	    		String submitstate=sws.querysubmitState(work.getWork_id(),student.getStu_id());
	    		if(submitstate.equals("yes")){
	    			submitstate="是";
	    		}
	    		else{
	    			submitstate="否";
	    		}
	    		record.setSubmitstate(submitstate);
	    		submitlist.add(record);
	    	}
		 }
	    
	     request.setAttribute("submitlist", submitlist);
	    return SUCCESS;
	}

}
